package com.xiaoyu.merchant.ui.activity.commodity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.xiaoyu.commonlib.ui.base.BaseActivity;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;
import com.xiaoyu.commonlib.utils.CommonFragmentPagerAdapter;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.commonlib.utils.UIUtils;
import com.xiaoyu.merchant.MerchantApplication;
import com.xiaoyu.merchant.MerchantConstants;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.ui.fragment.goods.GoodsCommentFragment;
import com.xiaoyu.merchant.ui.fragment.goods.GoodsDetailFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2019/2/13
 * @Description 商品详情页面
 */
public class CommodityDetailActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.activity_goods_detail_function_layout)
    LinearLayout functionLayout;//功能容器
    @BindView(R.id.activity_goods_detail_function_line)
    View mLine;//标志线
    @BindView(R.id.activity_goods_detail_view_page)
    ViewPager mViewPager;
    @BindView(R.id.activity_goods_detail_title_back_btn)
    ImageView backBtn;//返回

    private CommonFragmentPagerAdapter mPagerAdapter;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private String goodsId;//商品id

    //功能标示线
    private float buttonWidth;//每一个切换title控件的宽度


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commodity_detail);
        ButterKnife.bind(this);
        goodsId = getIntent().getStringExtra(MerchantConstants.GOODS_ID);
        Log.i("xiaoyuu", "goods id = " + goodsId);
        initView();
    }

    private void initView() {
        //商品详情fragment
        GoodsDetailFragment detailFragment = new GoodsDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString(MerchantConstants.GOODS_ID, goodsId);
        detailFragment.setArguments(bundle);

        //评价fragment
        GoodsCommentFragment commentFragment = new GoodsCommentFragment();
        Bundle bundle1 = new Bundle();
        bundle1.putString(MerchantConstants.GOODS_ID, goodsId);
        commentFragment.setArguments(bundle1);
        mFragmentList.clear();
        mFragmentList.add(detailFragment);
        mFragmentList.add(commentFragment);

        //viewpager
        mPagerAdapter = new CommonFragmentPagerAdapter(getSupportFragmentManager(), mFragmentList);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(onPageChangeListener);

        //功能项、标识线
        initLine();

        for (int i = 0; i < functionLayout.getChildCount(); i++) {
            functionLayout.getChildAt(i).setOnClickListener(this);
        }
        backBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v == functionLayout.getChildAt(0)) {//商品详情
            selectItem(0);
            scrollLineMove(0, 0);
            mViewPager.setCurrentItem(0);
        } else if (v == functionLayout.getChildAt(1)) {//累计评价
            selectItem(1);
            scrollLineMove(1, 0);
            mViewPager.setCurrentItem(1);
        } else if (v == backBtn) {//返回
            onBackPressed();
        }
    }


    /**
     * viewpager 滑动监听
     */
    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            scrollLineMove(position, positionOffset);
        }

        @Override
        public void onPageSelected(int position) {
            selectItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    public void initLine() {
        buttonWidth = functionLayout.getWidth() / functionLayout.getChildCount();
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLine.getLayoutParams();
        params.width = UIUtils.dip2px(50);
        params.leftMargin = (int) ((buttonWidth - params.width) / 2 + functionLayout.getLeft());
        mLine.setLayoutParams(params);

        Log.i("jiangxue", "func = " + functionLayout.getWidth() + "   left = " + functionLayout.getLeft()
                + "   button = " + buttonWidth);
    }


    /**
     * 设置功能标志线位置
     *
     * @param position
     */
    public void scrollLineMove(int position, float offset) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mLine.getLayoutParams();
        int leftMargin = (int) ((buttonWidth - params.width) / 2 + buttonWidth * (position + offset) + functionLayout.getLeft());
        params.leftMargin = leftMargin;
        mLine.setLayoutParams(params);
    }

    /**
     * 设置功能titlebar选项的文字颜色
     *
     * @param position
     */
    public void selectItem(int position) {
        for (int i = 0; i < functionLayout.getChildCount(); i++) {
            TextView view = (TextView) functionLayout.getChildAt(i);
            if (i == position) {
                view.setTextColor(Color.parseColor("#2796f0"));
            } else {
                view.setTextColor(Color.parseColor("#333333"));
            }
        }
    }
}
